import { http } from '@/utils/request'
import type {
  SysListUserParamsType,
  SysListUserType,
  SysUserDetailsType,
  SysUserEditType,
  SysUserListSelectType,
  SysUserWriteParamsType
} from './model'
import qs from 'qs'
import type { ExportExcelParams } from '@/views/admin/exportExcel/model'

/**
 * 分页获取用户列表
 * @param data 请求参数
 * @returns SysListUserType
 */
export const getUserListAPI = (data?: SysListUserParamsType) => {
  return http.request<PageResult<SysListUserType>>('get', '/sys_user', {
    params: data,
    paramsSerializer: (params) => {
      return qs.stringify(params, { arrayFormat: 'repeat' })
    }
  })
}

/**
 * 获取用户下拉列表
 * @returns SysUserListSelectType
 */
export const getUserSelectAPI = () => {
  return http.request<SysUserListSelectType[]>('get', '/sys_user/select')
}

/**
 * 获取用户详情
 * @param id 用户ID
 * @returns SysUserDetailsType
 */
export const getUserDetailsAPI = (id: number) => {
  return http.request<SysUserDetailsType>('get', `/sys_user/${id}`)
}

/**
 * 创建用户
 * @param data 请求参数
 * @returns SysUserDetailsType
 */
export const addSysUserAPI = (data: SysUserWriteParamsType) => {
  return http.request<SysUserDetailsType>('post', '/sys_user', { data })
}

/**
 * 更新用户
 * @param id 用户ID
 * @param data 请求参数
 * @returns SysUserDetailsType
 */
export const updateSysUserAPI = (id: number, data: SysUserWriteParamsType) => {
  return http.request<SysUserDetailsType>('put', `/sys_user/${id}`, { data })
}

/** 删除用户
 * @param id 用户ID
 * @returns Void
 */
export const deleteSysUserAPI = (id: number) => {
  return http.request<void>('delete', `/sys_user/${id}`)
}

/**
 * 修改用户信息
 * @param data
 */
export const updateUserInfo = (data: SysUserEditType) => {
  return http.request('put', '/sys_user/update/userInfo', { data })
}
/**
 * 修改用户密码
 * @param data
 */
export const updateUserPassword = (data: { newPassword: string; confirmPassword: string }) => {
  return http.request('put', '/sys_user/update/password', { data })
}

/**
 * 导出用户列表
 * @param data 请求参数
 */
export const exportUserExcelAPI = (data: ExportExcelParams<SysListUserParamsType>) => {
  return http.request(
    'post',
    '/sys_user/export',
    { data },
    false,
    {
      responseType: 'blob'
    },
    true
  )
}
